package com.tianqu.tms.www.car.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn30400Bo;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.MarketCarGateInfo;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.car.viewhelper.CarInnerRegisterViewhelper;

/**
 * @author zhangxueliang
 * 出库登记确认controller
 */
public class CarOutRegisterResultController extends TmsController{
	private Logger logger = Logger.getLogger(CarOutRegisterResultController.class);
	
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			Car car = Car.getInstance();
			CarInnerRegisterViewhelper viewhelper = new CarInnerRegisterViewhelper();
			viewhelper = (CarInnerRegisterViewhelper)uc.getAttribute(TmsConstants.VIEWHELP);
			TmsUserProfile profile = uc.getUserProfile();
			
			//车辆编号
			String serial = viewhelper.getBasicCarId();
			//库存状态
			int type = 2;
			car.setSerial(serial);
			//更新入库状态
			car.updateRegisterStatus(car, type);
			
			car = car.findCarBySerial(serial);
			Member member = Member.getInstance();			
			member = member.findCarScoreByMemberId(String.valueOf(car.getMemberId()));			
			int usedCarPort = 0;
			int increment = 0;
			//当前库存量大于0时
			if(member.getUsedCarPort() > 0){			
				usedCarPort = member.getUsedCarPort() - 1;
				increment = -1;
			}
			//更新会员表的当前库存量
			member.updateUsedCarPort(Integer.valueOf(viewhelper.getMemberId()),member.getUsedCarPort(), increment ,"CarOutRegisterResult");
			
			//入库记录增加新的一条信息
			MarketCarGateInfo marketCarGateInfo = MarketCarGateInfo.getInstance();
			marketCarGateInfo.setSerial(viewhelper.getBasicCarId());
			marketCarGateInfo.setGateId(0);
			marketCarGateInfo.setLicensePlate(car.getLicensePlate());
			marketCarGateInfo.setTempLicensePlate(car.getTempLicensePlate());
			marketCarGateInfo.setGateType("人工出库登记");
			marketCarGateInfo.setLocatedIP("");
			marketCarGateInfo.setCarPhoto("");
			marketCarGateInfo.setCarColor("0");
			marketCarGateInfo.setLicenseColor(0);
			marketCarGateInfo.setThreadType(3);//人工处理
			marketCarGateInfo.setThreadStatus(1);//人工处理成功
			marketCarGateInfo.setCheckFlag(2);
			marketCarGateInfo.setUpdateUserId(profile.getUniqueId());
			marketCarGateInfo.insertGateInfo(marketCarGateInfo);
			
			car.setSerial(viewhelper.getBasicCarId());			
			car.setHandleType(8);//人工出库
			car.setIsPush(2);
			car.setPushContent("");
			car.setUpdateUserId(profile.getUniqueId());
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式 
			String updataTime = DateUtil.getCurrentTime();
			car.setUpdateTime(df.parse(updataTime));
			
			//插入库存车辆特殊处理记录表
			car.insertCarHandleInfo(car);
			
			controller.commit();
			
//			微信同步
//			MobileMessage message = new MobileMessage();
//			message.putValue("MemberId",String.valueOf(viewhelper.getMemberId()));
//			message.setMessage("TMS出库登记确认无法同步[入出库登记]接口,会员ID为#MemberId#");
//			
//			ChangeRegisterStatusBo api = new ChangeRegisterStatusBo();
//			api.setMobileMessage(message);
//			api.setSendMail(true);
//			api.setSerial(car.getSerial());
//			api.setMemberId(String.valueOf(viewhelper.getMemberId()));
//			api.setRegisterStatus(2);
//			PullingExecutor.getInstance().submit(api);
			
			//4、【TMS】出库登记/接口出库登记 =》【车代办】同步
			MobileMessage message = new MobileMessage();
			message.putValue("Serial",serial);
			message.setMessage("【TMS】出库登记/接口出库登记 =》【车代办】同步,Serial为#Serial#");
			
			 TransferDataSyn30400Bo api = new TransferDataSyn30400Bo();
			 api.setMobileMessage(message);
			 api.setRegisterStatus(type);
			 api.setSerial(serial);
			 api.setUsedCarPort(usedCarPort);
			 api.setMarketNo(TmsConstants.MARKET_NO);
			 api.setEnterTime(new Date().getTime());
			 PullingExecutor.getInstance().submit(api);
			
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
		
	}

}

